<template>

  <div class="topbar">
    <div class="wrap">
      <div class="left">欢迎来到叩丁狼积分商城</div>

      <ul class="right">
        <li>
          <!-- {{userInfo.headImg}} -->
          <img :src="userInfo.headImg" alt="">
          用户名：{{userInfo.nickName}}
        </li>
        <li>我的积分：{{userInfo.coin}}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="loginbtn" @click="changeIsShowLoginModal(true)" v-if="!isLogined">登录</li>
        <li class="cart-btn" v-else>
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{cartTotal}}</strong>
        </li>
      </ul>
    </div>
  </div>

</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex"
import { weixinLoginAPI, ShopCartsAPI } from "@/request/api"
export default {
  created() {
    console.log("topbarkey发生了改变",);

    this.$router.onReady(async () => {
      let paramscode = this.$route.query.code
      if (paramscode) {
        let res = await weixinLoginAPI({
          code: paramscode
        })
        console.log("123", res);
        if (res.code == 0) {
          // 提示登录成功
          alert("登录成功！")
          // 存储token
          localStorage.setItem("x-auth-token", res["x-auth-token"])
          // 更新登录状态
          this.changeIsLogined(true)
          // 删除地址栏中的code
          this.$router.push(this.$route.path)
          // 成功
          this.asyncchangeUserInfo()
        } else if (res.code == 400) {
          // 二维码失效
          // 提示重新扫码
          alert("请重新扫码")
          // 弹出登录框 
          this.changeIsShowLoginModal(true)
        } else if (res.code == 407) {
          // 1 提示第一次登录
          alert("请使用手机号再绑定一次！")
          // this.asyncIsShowToast({
          //   isshow: true,
          //   msg: "请使用手机号再绑定一次！",
          //   type: "danger"
          // })
          // 2 弹出登录框
          this.changeIsShowLoginModal(true)
          // 3 本地存储uuid
          localStorage.setItem("uuid", res.uuid)
          // 4 清除地址栏中的code
          this.$router.push(this.$route.path)
        }

      } else {
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(token))
        this.initUserInfo(Boolean(token))
        if (token) {
          this.asyncchangeUserInfo()
        }
      }
    })
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "logModal/changeIsShowLoginModal",
      changeIsLogined: "Logined/changeIsLogined",
      initUserInfo: "userInfo/initUserInfo"
    }),
    ...mapActions({
      asyncchangeUserInfo: "userInfo/asyncchangeUserInfo"
    })
  },
  computed: {
    ...mapState({
      isLogined: state => state.Logined.isLogined,
      userInfo: state => state.userInfo.userInfo,
      cartTotal: state => state.userInfo.cartTotal,
    })
  },
  watch: {
    // "$route.path": {
    // handler(newVal, oldVal) {
    // console.log("地址变了。。。");

    // let token = localStorage.getItem("x-auth-token")
    // 普通判断
    // if(token){
    //   this.changeIsLogined(true)
    // }else{
    //   this.changeIsLogined(false)
    // }
    // 转换布尔值判断
    // this.changeIsLogined(Boolean(token))
    // 三元判断
    // token ? this.changeIsLogined(true) : this.changeIsLogined(false)

    // }
    // }
  }
}
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242B39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #FFFEFE;

  .wrap {
    display: flex;
    justify-content: space-between;

    .right {
      display: flex;

      li {
        margin: 0 10px;
        text-align: center;
        cursor: pointer;

        img {
          width: 26px;
          height: 26px;
          border-radius: 13px;
          vertical-align: middle;
        }

        &.loginbtn {
          width: 124px;
          height: 40px;
          background: #0A328E;


        }

        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;

          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }

          span {
            margin: 0 8px;
          }

          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
    }
  }
}
</style>